package com.gopay.webtest.controller;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.FundSignHandler;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.vo.FundBindCardParamVO;
import com.gopay.webtest.vo.FundBindParamVO;
import com.gopay.webtest.vo.FundFileNotifyParamVO;
import com.gopay.webtest.vo.FundQueryCardInfoParamVO;
import com.gopay.webtest.vo.FundSubscriptionParamVO;
import com.gopay.webtest.xml.XmlDao;

/**
 * 基金发往gatewate工具类
 * @FundController.java
 * @author dong-wang5@hnari.com
 * @2016年6月16日 下午4:02:36  www.gopay.com.cn Inc.All rights reserved.
 */
@RequestMapping("/fund")
@Controller
public class FundController extends BaseController {
    @Resource(name = "xmlDao")
    private XmlDao xmlDao;

    @Resource(name = "fundSignHandler")
    private FundSignHandler fundSignHandler;

    //***************************************认申购开始************************************************
    /**
     *跳转到申购\认购页面
     * @return
     */
    @RequestMapping("/subscription")
    public ModelAndView subscription() {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_subscription", "subscription");
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if (map.containsKey("gopayServerTime")) {
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            mv = new ModelAndView("fund.subscription");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到申购认购页面", e);
        }
        return mv;
    }
    
    @RequestMapping("/doSubscription")
    public ModelAndView doSubscription(@ModelAttribute FundSubscriptionParamVO paramVo) {
        ModelAndView mv = new ModelAndView();
        try {
            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
           // paramVo.setGopayServerTime(DateUtils.format(new Date(),"yyyyMMddHHmmss"));
            // 签名
            fundSignHandler.sign(paramVo.getTranCode() + "_" + paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());

            mv.setViewName("fund.doSubscription");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("执行申购认购", e);
        }
        return mv;
    }

    @RequestMapping("/subscriptionQuery")
    public ModelAndView subscriptionQuery() {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_subscriptionQuery", "subscriptionQuery");
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            mv = new ModelAndView("fund.subscriptionQuery");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到申购认购查询页面", e);
        }
        return mv;
    }
    
    @RequestMapping("/doSubscriptionQuery")
    public ModelAndView doSubscriptionQuery(@ModelAttribute FundSubscriptionParamVO paramVo) {
        ModelAndView mv = new ModelAndView();
        try {
            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
            paramVo.setGopayServerTime(DateUtils.format(new Date(),"yyyyMMddHHmmss"));
            // 签名
            fundSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            mv.setViewName("fund.doSubscriptionQuery");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("执行申购认购查询", e);
        }
        return mv;
    }
    
    //***************************************认申购结束************************************************

    /**
     * 基金公司文件上传通知
     * @return
     */
    @RequestMapping("/fundFileNotify")
    public ModelAndView fundFileNotify() {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_fundFileNotify", "fundFileNotify");
            if (map.containsKey("fileUploadTime")) {
                map.remove("fileUploadTime");
                map.put("fileUploadTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            mv = new ModelAndView("fund.fundFileNotify");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到基金公司文件上传通知页面", e);
        }

        return mv;
    }

    @RequestMapping("/doFundFileNotify")
    public ModelAndView doFundFileNotify(@ModelAttribute FundFileNotifyParamVO paramVo) {
        ModelAndView mv = new ModelAndView();
        try {
            // 签名
            fundSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            mv.setViewName("fund.doFundFileNotify");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("执行基金公司文件上传通知", e);
        }
        return mv;
    }

    //***************************************签约开始************************************************
    /**
     * 跳转到签约页面
     * 
     * @return
     */
    @RequestMapping("/bind")
    public ModelAndView bind() {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_bind", "bind");
            
            Set<String> set = new HashSet<String>();
            set.add("tranDateTime");
            set.add("gopayServerTime");
            Iterator<String> itr = set.iterator();
            while(itr.hasNext()){
                String time = itr.next();
                if(map.containsKey(time)){
                    map.remove(time);
                    map.put(time, DateUtils.format(new Date(), "yyyyMMddHHmmss"));
                }
            }
            
            mv = new ModelAndView("fund.bind");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到签约页面", e);
        }
        return mv;
    }
    
    /**
     * 执行签约
     * 
     * @return
     */
    @RequestMapping("/doBind")
    public ModelAndView doBind(@ModelAttribute FundBindParamVO paramVo) {
        ModelAndView mv = new ModelAndView();
        try {
            String backgroundMerUrl = paramVo.getBackgroundMerUrl()==null?"":paramVo.getBackgroundMerUrl();
           /* if(backgroundMerUrl!=null&&!backgroundMerUrl.equals("")){
                backgroundMerUrl = backgroundMerUrl+ "?id="+ paramVo.getTranIP();
            }*/
            paramVo.setBackgroundMerUrl(backgroundMerUrl);
            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
            //paramVo.setGopayServerTime(paramVo.getTranDateTime());
            // 签名
            fundSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            mv.setViewName("fund.doBind");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("处理签约请求", e);
        }
        return mv;
    }
    
    /**
     * 跳转到签约查询页面
     *
     * @return
     */
    @RequestMapping("/bindFind")
    public ModelAndView bindFind() {
        ModelAndView mv = new ModelAndView();

        try {
            Map<String, String> map = xmlDao.readData4Map("fund_bindFind", "bindFind");
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            mv = new ModelAndView("fund.bindFind");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到签约查询页面", e);
        }
        return mv;
    }
    
    /**
     * 执行签约查询
     *
     * @return
     */
    @RequestMapping("/doBindFind")
    public ModelAndView doBindFind(@ModelAttribute FundBindParamVO paramVo) {
        ModelAndView mv = new ModelAndView();

        try {
            String backgroundMerUrl = paramVo.getBackgroundMerUrl();
            paramVo.setBackgroundMerUrl(backgroundMerUrl);

            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
           // paramVo.setGopayServerTime(paramVo.getTranDateTime());
            // 签名
            fundSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            mv.setViewName("fund.doBindFind");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("处理签约查询请求", e);
        }
        return mv;
    }
//    ***************************************签约结束************************************************
    
//    ***************************************解约开始************************************************
    /**
     * 跳转到解约页面
     *
     * @return
     */
    @RequestMapping("/unbind")
    public ModelAndView unbind() {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_unbind", "bind");
            
            Set<String> set = new HashSet<String>();
            set.add("tranDateTime");
            set.add("gopayServerTime");
            Iterator<String> itr = set.iterator();
            while(itr.hasNext()){
                String time = itr.next();
                if(map.containsKey(time)){
                    map.remove(time);
                    map.put(time, DateUtils.format(new Date(), "yyyyMMddHHmmss"));
                }
            }
            
            mv = new ModelAndView("fund.unbind");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到解约页面", e);
        }
        return mv;
    }
    
    /**
     * 执行解约查询
     *
     * @return
     */
    @RequestMapping("/doUnbind")
    public ModelAndView doUnbind(@ModelAttribute FundBindParamVO paramVo) {
        ModelAndView mv = new ModelAndView();
        try {
            // 签名
            fundSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            mv.setViewName("fund.doUnbind");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("执行解约操作", e);
        }
        return mv;
    }
//    ***************************************解约结束************************************************
    
//    ***************************************短信发送开始************************************************
    
    /**
     * 短信发送
     * @return
     * @author weixunfang
     * @2016年7月19日 上午11:19:40
     */
    @RequestMapping("/sendMsg")
    public ModelAndView sendMsg() {
        ModelAndView mv = new ModelAndView();

        try {
            Map<String, String> map = xmlDao.readData4Map("fund_sendMsg", "bind");
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if(map.containsKey("gopayServerTime")){
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(),"yyyyMMddHHmmss"));
            }
            mv = new ModelAndView("fund.sendMsg");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到短信发送页面", e);
        }
        return mv;
    }

    /**
     * 执行发送短信
     *
     * @return
     */
    @RequestMapping("/doSendMsg")
    public ModelAndView doSendMsg(@ModelAttribute FundBindParamVO paramVo) {
        ModelAndView mv = new ModelAndView();
        try {
            String backgroundMerUrl = paramVo.getBackgroundMerUrl();
            if(backgroundMerUrl!=null&&!backgroundMerUrl.equals("")){
                backgroundMerUrl = backgroundMerUrl+ "?id="+ paramVo.getTranIP();
            }
            paramVo.setBackgroundMerUrl(backgroundMerUrl);
            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
            //paramVo.setGopayServerTime(paramVo.getTranDateTime());
            // 签名
            fundSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            mv.setViewName("fund.doSendMsg");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("处理短信发送请求", e);
        }
        return mv;
    }
//    ***************************************短信发送结束************************************************

//    ***************************************绑卡开始************************************************
    /**
     * 绑卡
     * @return
     */
     @RequestMapping("/bindCard")
    public ModelAndView bindCard() {
        ModelAndView modelAndView = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_bindcard", "bind");
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            modelAndView = new ModelAndView("fund.bindCard");
            modelAndView.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到绑卡页面", e);
        }
        return modelAndView;
    }

    /**
     * 基金执行解绑卡业务
     * @uncle Bai
     * @param paramVo
     * @return
     */
    @RequestMapping("/doBindCard")
    public ModelAndView doBindCard(@ModelAttribute FundBindCardParamVO paramVo) {
        ModelAndView modelAndView = new ModelAndView();
        try {
            paramVo.setGopayServerTime(DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            // 签名
            fundSignHandler.sign(paramVo.getTranCode() + "_" + paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            modelAndView.setViewName("/fund/doBindCard");
            modelAndView.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("在执行绑卡时候出现错误", e);
        }
        return modelAndView;
        }

    /**
     * 基金卡查询
     * @Uncle Bai
     * @return
     */
    @RequestMapping("/queryCardInfo")
    public ModelAndView queryCardInfo() {
        ModelAndView modelAndView = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_queryCardInfo", "bind");
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            modelAndView = new ModelAndView("fund.queryCardInfo");
            modelAndView.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到绑卡页面", e);
        }
        return modelAndView;
    }

    /**
     * 基金执行卡查询业务
     * @uncle Bai
     * @param paramVo
     * @return
     */
    @RequestMapping("/doQueryCardInfo")
    public ModelAndView doQueryCardInfo(@ModelAttribute FundQueryCardInfoParamVO paramVo) {
        ModelAndView modelAndView = new ModelAndView();
        try {
            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
                paramVo.setGopayServerTime(DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            // 签名
            fundSignHandler.sign(paramVo.getTranCode() + "_" + paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            modelAndView.setViewName("/fund/doQueryCardInfo");
            modelAndView.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("在执行卡查询时候出现错误", e);
        }
        return modelAndView;
    }
//    ***************************************绑卡结束************************************************


    /**项目名称 : 基金二期优化:新增签约查询接口   创建日期 : 17/1/24 10:20  Author : baiyunpeng  start*/
    @RequestMapping("/signQuery")
    public ModelAndView signQuery() {
        ModelAndView mv = new ModelAndView();

        try {
            Map<String, String> map = xmlDao.readData4Map("fund_signQuery", "signQuery");
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            mv = new ModelAndView("fund.signQuery");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到签约查询页面", e);
        }
        return mv;
    }


    @RequestMapping("/doSignQuery")
    public ModelAndView doSignQuery(@ModelAttribute FundBindParamVO paramVo) {
        ModelAndView mv = new ModelAndView();

        try {
            String backgroundMerUrl = paramVo.getBackgroundMerUrl();
            paramVo.setBackgroundMerUrl(backgroundMerUrl);
            fundSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            mv.setViewName("fund.doSignQuery");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("处理签约查询请求", e);
        }
        return mv;
    }
    /**项目名称 : 基金二期优化:新增签约查询接口   创建日期 : 17/1/24 10:20  Author : baiyunpeng  end*/
}
